package com.zoro.barn.business.service.system.business.menu.service;

import com.zoro.barn.business.service.system.business.menu.entiity.BarnMenu;
import com.zoro.barn.business.service.system.business.menu.dao.BarnMenuMapper;
import com.zoro.barn.business.service.system.business.rolemenu.dao.BarnRoleMenuMapper;
import com.zoro.barn.commons.tools.string.IDs;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import jakarta.annotation.Resource;
import java.util.*;
import java.util.stream.Collectors;

/**
 * @author zhaoxingwu
 */
@Service
public class BarnMenuService {

    @Resource
    private BarnMenuMapper barnMenuMapper;
    @Resource
    private BarnRoleMenuMapper barnRoleMenuMapper;

    /**
     * 查询全部数据
     *
     * @return result set
     */
    public List<BarnMenu> getAll() {
        return this.barnMenuMapper.getAll();
    }

    /**
     * 查询一条数据
     *
     * @param pid pid
     * @return data
     */
    public BarnMenu getById(String pid) {
        return this.barnMenuMapper.getById(pid);
    }

    /**
     * 插入一条新数据
     *
     * @param data data
     * @return saved data
     */
    @Transactional(rollbackFor = Exception.class)
    public BarnMenu insert(BarnMenu data) {
        data.setPid(IDs.uuid());
        data.setCreateTime(new Date());
        this.barnMenuMapper.insert(data);
        return data;
    }

    /**
     * update one record
     *
     * @param data new record data
     * @return saved data
     */
    @Transactional(rollbackFor = Exception.class)
    public BarnMenu update(BarnMenu data) {
        data.setUpdateTime(new Date());
        this.barnMenuMapper.update(data);
        return data;
    }

    /**
     * delete one record by primary id and all its children node
     *
     * @param pid pid
     */
    @Transactional(rollbackFor = Exception.class)
    public void delete(String pid) {
        if (StringUtils.isNotBlank(pid)) {
            this.barnMenuMapper.delete(pid);
            this.barnMenuMapper.deleteByParent(pid);
            this.barnRoleMenuMapper.deleteByMenu(pid);
        }
    }

    /**
     * 查询用户，和团队所对应的 菜单
     *
     * @param userId  user
     * @param teamIds team
     * @return result
     */
    public List<BarnMenu> getByUserId(String userId, Set<String> teamIds) {
        // 1. 查询其拥有的角色的菜单
        // 2. 查询其团队默认的角色的菜单

        List<BarnMenu> userMenus = this.barnMenuMapper.getByUserId(userId);
        List<BarnMenu> resultList = new ArrayList<>(userMenus);
        if (CollectionUtils.isNotEmpty(teamIds)) {
            List<BarnMenu> teamMenus = this.barnMenuMapper.getByTeamIds(teamIds);
            resultList.addAll(teamMenus);
        }
        if (CollectionUtils.isNotEmpty(resultList)) {
            // 去除重复
            Collection<BarnMenu> result = resultList.stream().collect(Collectors.toMap(BarnMenu::getPid, v -> v, (a, b) -> b)).values();
            return new ArrayList<>(result);
        } else {
            return null;
        }

    }


}
